﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:MyCalManager.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
  
  OR (WPF only):
  
  xmlns:vm="clr-namespace:MyCalManager.ViewModel"
  DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
*/

namespace MyCalManager.ViewModel
{
    
    public class ViewModelLocator
    {
        private static MainViewModel _main;

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            CreateMain();
        }

        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
            ClearMain();
            ClearMainContent();
            ClearUserView();
            ClearNewDay();
            ClearMonthView();
            ClearYearView();
            ClearAbout();
        }

        #region MainViewModel

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static MainViewModel MainStatic
        {
            get
            {
                if (_main == null)
                {
                    CreateMain();
                }

                return _main;
            }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearMain()
        {
            _main.Cleanup();
            _main = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Main property.
        /// </summary>
        public static void CreateMain()
        {
            if (_main == null)
            {
                _main = new MainViewModel();
            }
        }

        #endregion

        #region MainModel


        private static MainContentModel _MainContent;

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        public static MainContentModel MainContentStatic
        {
            get
            {
                if (_MainContent == null)
                {
                    CreateMainContentModel();
                }

                return _MainContent;
            }
        }

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainContentModel MainContent
        {
            get
            {
                return MainContentStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the ViewModelPropertyName property.
        /// </summary>
        public static void ClearMainContent()
        {
            if (_MainContent != null)
            {
                _MainContent.Cleanup();
                _MainContent = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the ViewModelPropertyName property.
        /// </summary>
        public static void CreateMainContentModel()
        {
            if (_MainContent == null)
            {
                _MainContent = new MainContentModel();
            }
        }

        #endregion
        
        #region UserViewModel

        private static UserViewModel _UserViewModel;

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        public static UserViewModel UserViewStatic
        {
            get
            {
                if (_UserViewModel == null)
                {
                    CreateUserViewModel();
                }

                return _UserViewModel;
            }
        }

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public UserViewModel UserView
        {
            get
            {
                return UserViewStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the ViewModelPropertyName property.
        /// </summary>
        public static void ClearUserView()
        {
            if (_UserViewModel != null)
            {
                _UserViewModel.Cleanup();
                _UserViewModel = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the ViewModelPropertyName property.
        /// </summary>
        public static void CreateUserViewModel()
        {
            if (_UserViewModel == null)
            {
                _UserViewModel = new  UserViewModel();
            }
        }

        #endregion
        
        #region NewDayViewModel

        private static NewDayViewModel _NewDayViewModel;

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        public static NewDayViewModel NewDayStatic
        {
            get
            {
                if (_NewDayViewModel == null)
                {
                    CreateNewDayViewModel();
                }

                return _NewDayViewModel;
            }
        }

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public NewDayViewModel NewDay
        {
            get
            {
                return NewDayStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the ViewModelPropertyName property.
        /// </summary>
        public static void ClearNewDay()
        {
            if (_NewDayViewModel != null)
            {
                _NewDayViewModel.Cleanup();
                _NewDayViewModel = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the ViewModelPropertyName property.
        /// </summary>
        public static void CreateNewDayViewModel()
        {
            if (_NewDayViewModel == null)
            {
                _NewDayViewModel = new NewDayViewModel();
            }
        }

        #endregion
        
        #region MonthViewModel

        private static MonthViewModel _MonthViewModel;

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        public static MonthViewModel MonthViewStatic
        {
            get
            {
                if (_MonthViewModel == null)
                {
                    CreateMonthViewModel();
                }

                return _MonthViewModel;
            }
        }

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MonthViewModel MonthView
        {
            get
            {
                return MonthViewStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the ViewModelPropertyName property.
        /// </summary>
        public static void ClearMonthView()
        {
            if (_MonthViewModel != null)
            {
                _MonthViewModel.Cleanup();
                _MonthViewModel = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the ViewModelPropertyName property.
        /// </summary>
        public static void CreateMonthViewModel()
        {
            if (_MonthViewModel == null)
            {
                _MonthViewModel = new MonthViewModel();
            }
        }

        #endregion

        #region YearViewModel

        private static YearViewModel _YearViewModel;

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        public static YearViewModel YearViewStatic
        {
            get
            {
                if (_YearViewModel == null)
                {
                    CreateYearViewModel();
                }

                return _YearViewModel;
            }
        }

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public YearViewModel YearView
        {
            get
            {
                return YearViewStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the ViewModelPropertyName property.
        /// </summary>
        public static void ClearYearView()
        {
            if (_YearViewModel != null)
            {
                _YearViewModel.Cleanup();
                _YearViewModel = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the ViewModelPropertyName property.
        /// </summary>
        public static void CreateYearViewModel()
        {
            if (_YearViewModel == null)
            {
                _YearViewModel = new YearViewModel();
            }
        }

        #endregion
        
        #region AboutViewModel

        private static AboutViewModel _AboutViewModel;

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        public static AboutViewModel AboutViewStatic
        {
            get
            {
                if (_AboutViewModel == null)
                {
                    CreateAboutViewModel();
                }

                return _AboutViewModel;
            }
        }

        /// <summary>
        /// Gets the ViewModelPropertyName property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public AboutViewModel About
        {
            get
            {
                return AboutViewStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the ViewModelPropertyName property.
        /// </summary>
        public static void ClearAbout()
        {
            if (_AboutViewModel != null)
            {
                _AboutViewModel.Cleanup();
                _AboutViewModel = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the ViewModelPropertyName property.
        /// </summary>
        public static void CreateAboutViewModel()
        {
            if (_AboutViewModel == null)
            {
                _AboutViewModel = new AboutViewModel();
            }
        }

        #endregion

                
    }
}